package service

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto/rpc"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetEpisodeById(c *gin.Context) {
	var form request.GetEpisodeById
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.GetEpisodeById(c, &__.GetEpisodeByIdReq{
		EpisodeId: int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "查询内容详情失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询内容详情完成", "data": gin.H{
		"list": video.List,
	}})
	return
}
func GetEpisodeList(c *gin.Context) {
	var form request.GetEpisodeList
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.GetEpisodeList(c, &__.GetEpisodeListReq{
		VideoId: int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "查询分集列表数据失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询分集列表数据完成", "data": gin.H{
		"Title":        video.Title,
		"Duration":     video.Duration,
		"EpisodeOrder": video.EpisodeOrder,
		"Refer":        video.Refer,
		"AuthEr":       video.AuthEr,
		"LookCount":    video.LookCount,
		"LikeCount":    video.LikeCount,
		"CommentCount": video.CommentCount,
		"CollectCount": video.CollectCount,
		"IsFree":       video.IsFree,
	}})
	return
}
func LookVideo(c *gin.Context) {
	var form request.LookVideo
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.LookVideo(c, &__.LookVideoReq{
		UserId:   int64(form.UserId),
		MemberId: int64(form.MemberId),
		VideoId:  int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询完成", "data": gin.H{
		"videoUrl": video.VideoUrl,
	}})
	return
}
func LookEpisode(c *gin.Context) {
	var form request.LookEpisode
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.LookEpisode(c, &__.LookEpisodeReq{
		VideoId:      int64(form.VideoId),
		EpisodeOrder: int64(form.EpisodeOrder),
		Duration:     int64(form.Duration),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "观看记录完成", "data": gin.H{
		"episode": video.LookEpisodeId,
	}})
	return
}

func GetEpisodeLogList(c *gin.Context) {
	var form request.GetEpisodeLogList
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.GetEpisodeLogList(c, &__.GetEpisodeLogListReq{
		Page: int64(form.Page),
		Size: int64(form.Size),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "观看记录完成", "data": gin.H{
		"list": video.List,
	}})
	return
}
